package com.pe1n.yangtaopai.view.activity;

import android.app.Fragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.pe1n.yangtaopai.R;
import com.pe1n.yangtaopai.bean.VersionBean;
import com.pe1n.yangtaopai.helper.InitRetrofitHelper;
import com.pe1n.yangtaopai.retrofitApi.RetrofitApi;
import com.pe1n.yangtaopai.service.VersionService;
import com.pe1n.yangtaopai.utils.URLConstant;
import com.pe1n.yangtaopai.view.fragment.AmountFragment;
import com.pe1n.yangtaopai.view.fragment.HomePageFragment;
import com.pe1n.yangtaopai.view.fragment.MyFragment;
import com.pe1n.yangtaopai.view.fragment.ShoppingFragment;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {
    private RadioGroup mRadioGroup_main;//下面的导航栏
    private FrameLayout mFrameLayout_main;//动态添加Fragment的容器
    private RadioButton mHomePageButton;//首页
    private RadioButton mShoppingButton;//购物车
    private RadioButton mAmountButton;//日本销量
    private RadioButton mMyButton;//我
    private FragmentManager mFragmentManager;//fragment的管理
    private FragmentTransaction mFragmentTransaction;
    private AmountFragment mAmountFragment;
    private HomePageFragment mHomePageFragment;
    private MyFragment mMyFragment;
    private ShoppingFragment mShoppingFragment;
    private Context mContext;
    private AlertDialog mAlertDialog;
    private PackageManager packageManager;
    private PackageInfo packageInfo;
    private int currentVersion;
    private String versionStr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initFrameLayout();
        initView();//初始化控件
        getCurrentVersionB();//得到当前的版本
        getNetVersion();
    }

    private void getNetVersion() {
        InitRetrofitHelper.getRetrofitInterfance().getVersionuBean(URLConstant.CURRENT_VERSION)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<VersionBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("oonError", "ws");
                    }

                    @Override
                    public void onNext(VersionBean versionBean) {
                        int version = versionBean.getData().getVersion();
                        versionStr = versionBean.getData().getVsersionUrl();
                        Log.e("onNextssdes", String.valueOf(version));
                        if (version > currentVersion) {
                            initDialog();
                        }

                    }
                });
    }

    private void getCurrentVersionB() {
        packageManager = this.getPackageManager();
        try {
            //获取当前的版本号
            packageInfo = packageManager.getPackageInfo(getPackageName(), 0);
            currentVersion = packageInfo.versionCode;
            Log.e("VERSION", String.valueOf(currentVersion));
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    private void initDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("检测到有新版本,你需要更新吗?")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(mContext, "下载中", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(mContext, VersionService.class);
                        intent.putExtra("VERSION_CURRENT", currentVersion);
                        intent.putExtra("VERSION_URL", versionStr);
                        startService(intent);

                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mAlertDialog.dismiss();
                    }
                });
        mAlertDialog = builder.create();
        mAlertDialog.show();
    }

    private void initFrameLayout() {
        mFragmentManager = getSupportFragmentManager();

    }

    private void initView() {
        mContext = this;
        mFrameLayout_main = (FrameLayout) findViewById(R.id.frameLayout_main);
        mRadioGroup_main = (RadioGroup) findViewById(R.id.radioGroup_main);
        mHomePageButton = (RadioButton) findViewById(R.id.homePageFragment_main);
        mAmountButton = (RadioButton) findViewById(R.id.amountFragment_main);
        mShoppingButton = (RadioButton) findViewById(R.id.shoppingFragment_main);
        mMyButton = (RadioButton) findViewById(R.id.myFragment_main);
        mRadioGroup_main.check(R.id.homePageFragment_main);
        mFragmentTransaction = mFragmentManager.beginTransaction();

        mHomePageFragment = new HomePageFragment();
        mFragmentTransaction.add(R.id.frameLayout_main, mHomePageFragment, "HomePageFragment");
        mFragmentTransaction.show(mHomePageFragment);

        mAmountFragment = new AmountFragment();
        mFragmentTransaction.add(R.id.frameLayout_main, mAmountFragment, "AmountFragment");
        mFragmentTransaction.hide(mAmountFragment);

        mShoppingFragment = new ShoppingFragment();
        mFragmentTransaction.add(R.id.frameLayout_main, mShoppingFragment, "ShoppingFragment");
        mFragmentTransaction.hide(mShoppingFragment);

        mMyFragment = new MyFragment();
        mFragmentTransaction.add(R.id.frameLayout_main, mMyFragment, "  MyFragment");
        mFragmentTransaction.hide(mMyFragment);
        mFragmentTransaction.commit();

        mRadioGroup_main.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.homePageFragment_main:
                        mFragmentTransaction = mFragmentManager.beginTransaction();
                        mFragmentTransaction.show(mHomePageFragment);
                        mFragmentTransaction.hide(mAmountFragment);
                        mFragmentTransaction.hide(mShoppingFragment);
                        mFragmentTransaction.hide(mMyFragment);
                        mFragmentTransaction.commit();
                        break;
                    case R.id.amountFragment_main:
                        mFragmentTransaction = mFragmentManager.beginTransaction();
                        mFragmentTransaction.hide(mHomePageFragment);
                        mFragmentTransaction.show(mAmountFragment);
                        mFragmentTransaction.hide(mShoppingFragment);
                        mFragmentTransaction.hide(mMyFragment);
                        mFragmentTransaction.commit();
                        break;
                    case R.id.shoppingFragment_main:
                        mFragmentTransaction = mFragmentManager.beginTransaction();
                        mFragmentTransaction.hide(mHomePageFragment);
                        mFragmentTransaction.hide(mAmountFragment);
                        mFragmentTransaction.show(mShoppingFragment);
                        mFragmentTransaction.hide(mMyFragment);
                        mFragmentTransaction.commit();
                        break;
                    case R.id.myFragment_main:
                        mFragmentTransaction = mFragmentManager.beginTransaction();
                        mFragmentTransaction.hide(mHomePageFragment);
                        mFragmentTransaction.hide(mAmountFragment);
                        mFragmentTransaction.hide(mShoppingFragment);
                        mFragmentTransaction.show(mMyFragment);
                        mFragmentTransaction.commit();
                        break;
                }
            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Intent intent = new Intent(mContext, VersionService.class);
        stopService(intent);
    }


}
